import {
  fetchHome,
  fetchIcon
} from '../../services/home/home';
import { Base_Api } from '../../config/index';
import {
  fetchGoodsList,
  fetchGoods
} from '../../services/good/fetchGoods';
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    icon_arr: [],
    gridItemImg_src: "https://cdn-we-retail.ym.tencent.com/tsr/goods/muy-3a.png?imageMogr2/thumbnail/585x585/quality/70/strip/format/webp",
    imgSrcs: [],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'scaleToFill'
    },
    goodListPagination: {
      index: 0,
      num: 20,
    },

    privateData: {
      tabIndex: 0,
    },
  },



  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    // this.loadHomePage();
    fetchIcon().then((res) => {

      res.data.value.map((item) => {
        item.img = Base_Api + item.img
      })
      this.setData({
        icon_arr: res.data.value
      })
    })

    this.loadGoodsList(true)



  },

  loadHomePage() {



    /*
    // wx.stopPullDownRefresh();

    // this.setData({
    //   pageLoading: true,
    // });
    // fetchHome().then(({
    //   swiper,
    //   tabList
    // }) => {
    //   let xxx = "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png"
    //   let arr = []
    //   for (let i = 0; i < 5; i++) {
    //     arr.push(xxx)
    //   }

    //   console.log("tableList", tabList)
    //   this.setData({
    //     tabList,
    //     imgSrcs: arr,
    //     pageLoading: false,
    //   });
    //   this.loadGoodsList(true);
    // });
    */
  },

  tabChangeHandle(e) {
    this.setData({
      'privateData.tabIndex': e.detail
    }, () => {
      this.loadGoodsList(true);
    })
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {



    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.data.goodListPagination.num;
    let pageIndex = 0
    if (!fresh) {
      pageIndex = this.data.privateData.tabIndex * pageSize + this.data.goodListPagination.index + 1;;
    }

    try {

      fetchGoods({
        pageIndex: pageIndex,
        pageSize: pageSize
      }).then((res) => {




        let goods_list = res.data.value.map((item) => {
          item.img = Base_Api + item.img
          item.thumb = Base_Api + item.thumb
          item.tags = item.tags.split(",")

          return item
        })

        this.setData({
          goodsList: fresh ? goods_list : this.data.goodsList.concat(goods_list),
          goodsListLoadStatus: 0,
        });

        this.data.goodListPagination.index = pageIndex;
        this.data.goodListPagination.num = pageSize;
      }).catch((err) => {
      })

    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  goodListClickHandle(e) {
    const {
      index
    } = e.detail;

    const {
      id
    } = this.data.goodsList[index];

    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${id}`,
    });
  },

  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  navToSearchPage() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    });
  },

  navToActivityDetail({
    detail
  }) {
    const {
      index: promotionID = 0
    } = detail || {};
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },
});